package com.glory.erp.invoice.goods.action;

import java.util.List;

import com.glory.erp.invoice.goods.service.GoodsService;
import com.glory.erp.invoice.goods.bean.Goods;
import com.glory.erp.invoice.goods.bean.GoodsQuery;
import com.glory.erp.invoice.goodstype.bean.GoodsType;
import com.glory.erp.invoice.goodstype.service.GoodsTypeService;
import com.glory.erp.invoice.supplier.bean.Supplier;
import com.glory.erp.invoice.supplier.service.SupplierService;
import com.glory.erp.auth.base.BaseAction;

public class GoodsAction extends BaseAction{
	
	public Goods goods = new Goods();
	public GoodsQuery goodsQuery = new GoodsQuery();

	private GoodsService goodsService;
	private SupplierService supplierService;
	private GoodsTypeService goodsTypeService;
	
	public void setGoodsTypeService(GoodsTypeService goodsTypeService) {
		this.goodsTypeService = goodsTypeService;
	}

	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}

	/**
	 * 
	 * @Description: 商品展示
	 * @Title: list
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月26日 下午3:06:25
	 * @throws
	 */
	public String list(){
		List<Supplier> supplierList = supplierService.findAll();
		put("supplierList", supplierList);
		setDataTotal(goodsService.getCount(goodsQuery));
		List<Goods> goodsList = goodsService.findAll(goodsQuery,pageNum,pageCount);
		put("goodsList", goodsList);
		return LIST;
	}

	/**
	 * 
	 * @Description: 跳转至商品新增、修改页面
	 * @Title: addView
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月26日 下午3:06:37
	 * @throws
	 */
	public String addView(){
		// 加载供应商信息
		List<Supplier> supplierList = supplierService.findAllUnion();
		put("supplierList", supplierList);
		// 加载第一个供应商对应的所有商品类别信息
		Long supplierUuid = null;
		if(goods.getUuid()!=null){
			goods = goodsService.findById(goods.getUuid());
			supplierUuid = goods.getGoodsType().getSupplier().getUuid();
		} else {
			supplierUuid = supplierList.get(0).getUuid();
		}
		List<GoodsType> goodsTypeList = goodsTypeService.findAllBySupplier(supplierUuid);
		put("goodsTypeList", goodsTypeList);
		return ADD;
	}

	/**
	 * 
	 * @Description: 新增、修改商品
	 * @Title: saveAndUpdate
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月26日 下午3:07:02
	 * @throws
	 */
	public String saveAndUpdate(){
		if(goods.getUuid() == null){
			goodsService.save(goods);
		}else{
			goodsService.update(goods);
		}
		return TO_LIST;
	}

	/**
	 * 
	 * @Description: 商品删除
	 * @Title: delete
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月26日 下午3:07:22
	 * @throws
	 */
	public String delete(){
		goodsService.delete(goods);
		return TO_LIST;
	}
}